﻿/*******************************************************
 *  医院管理 相关操作js
 * <p>Title: hosManager.js</p>
 * <p>Description:TODO</p>
 * @author Joychen
 * @date 2017年12月22日 下午5:41:57
 * @version 1.0
 * *****************************************************/
//医院管理
var unitzTree, selNode, folder_path,
     issetOut = false,
     options,
     $table, addOrEditUrl;
var addIndex;
var hosManager = {
    //初始化页面
    initPage: function () {
        parent.showLoadBoxs();
        hosManager.bindZtree();
        hosManager.initSelect();        
        hosManager.initTable();
        hosManager.bindEvent();
        parent.delLoadBoxs();
    },
    //初始化下拉
    initSelect:function(){
        //登录操作者
        $.ajax({
            url: ctx + '/BaneHospital/GetLoginUserData',
            type: "post",
            async: false,
            dataType: 'json',
            success: function (data) {
                if (data.Statu != 0) {
                    layer.msg(data.Msg, { icon: 5 });
                    return false;
                }
                var itemStr = "";
                $.each(data.Data, function (i, item) {
                    itemStr += "<option value='" + item.userId + "'>" + item.userName + "</option>";
                });
                $('#user_id').append(itemStr);
                $('#user_id').selectpicker('refresh');
            }
        });
        $('.selectpicker').selectpicker({ size: 6 });
    },
    //初始化单位树
    bindZtree: function () {
        var setting = {
            view: {
                showIcon: true,
                selectedMulti: false,
                fontCss: getFontCss
            },
            edit: {
                enable: true,
                showRemoveBtn: false,
                showRenameBtn: false
            },
            async: {
                enable: true,
                dataType: "text",
                type: "post",
                url: ctx + '/SysOrg/GetOrgTreeDataByRelation',
                autoParam: ["id"]
            },
            data: {
                key: { title: "name", name: "name" },
                simpleData: { enable: true, idKey: "id", pIdKey: "pId", rootPId: 0 }
            },
            check: {
                enable: false,
                autoCheckTrigger: true,
                chkStyle: "checkbox",
                chkboxType: { "Y": "p", "N": "ps" }
            },
            callback: {
                onAsyncError: function () {
                    layer.msg("初始化单位结构树失败~", { icon: 5 });
                },
                onAsyncSuccess: function (event, treeId, treeNode, msg) {
                    //树加载完成后执行
                    var obj = unitzTree.getNodes()[0];
                    if (obj) {
                        issetOut = true;
                        unitzTree.selectNode(obj);
                        unitzTree.setting.callback.onClick(null, unitzTree.setting.treeId, obj);//模拟点击事件 
                        if (obj.children && obj.children.length > 0)
                            unitzTree.expandNode(obj);
                    }
                },
                onClick: function (event, treeId, treeNode) {
                    $('#unitTree a').css("color", "#333");
                    $('#' + treeNode.tId + '_a').css("color", "#ffffff");
                    selNode = treeNode;//选中节点
                    folder_path = treeNode.folder_path;
                    if (issetOut)
                    { setTimeout(function () { $table.bootstrapTable('refresh'); issetOut = false; }, 1000); }
                    else
                        $table.bootstrapTable('refresh');
                }
            }
        };
        unitzTree = $("#unitTree").initzTreeView(setting, 'ushow');
    },
    //初始化Table
    initTable: function () {
         options = {
            url: ctx + "/BaneHospital/GetBaneHospitalData",
            cutHeight: 5,
            toolbar: "#exampleToolbar",
            queryParams: function (params) {
                params = {
                    rows: params.limit,
                    //页面大小  
                    page: params.offset / params.limit + 1,
                    folder_path: folder_path,
                    keyword: encodeURI($('#keyword').val())//医院名称
                }
                return params;
            },
            columns: [
            {
                radio: true
            },
	        {
	            field: '',//第一列序号
	            title: '序号',
	            align: 'center',
	            width: 50,
	            formatter: function (value, row, index) {
	                return index + 1;
	            }
	        },
	        {
	            field: 'hos_name',
	            title: '医院名',
	            align: 'center'
	        },
	        {
	            field: 'iris_code',
	            title: '虹膜机ID',
	            align: 'center'
	        },
	        {
	            field: 'user_name',
	            title: '虹膜机登录账户',
	            align: 'center'
	        }, {
	            field: "hos_note",
	            title: '备注',
	            align: 'center'
	        }],
            onClickRow: function (row, $element) {

            }
        }
        $table = tableHelper.initTable("exampleTableToolbar", options);
    },
    //绑定默认事件
    bindEvent: function () {
        //查询
        $('#btnSearch').click(function () {
            $table.bootstrapTable('refresh', options);
        });
        //添加
        $('#hosAdd').click(function () {
            $('#user_name').val('');//操作者
            $('#folder_path').val('');
            if (!folder_path) {
                layer.msg("添加医院前请选择组织机构！", {icon:5});
                return false;
            }
            hosManager.editForm();
        });
        //编辑
        $('#hosEdit').click(function () {
            var row = $table.bootstrapTable('getSelections');
            if (row != null && row.length == 0) {
                layer.msg("未选中行~", { icon: 5 });
                return false;
            }
            hosManager.editForm(row[0]);
        });
        //删除
        $('#hosDel').click(function () {
            var row = $table.bootstrapTable('getSelections');
            if (row != null && row.length == 0) {
                layer.msg("未选中记录行~", { icon: 5 });
                return false;
            }
            layer.confirm('您确定要删除当前选择医院？', {
                btn: ['确定', '取消'] //按钮
            }, function () {
                var index = layer.msg("删除中...", { icon: 6, time: 6000 });
                $.ajax({
                    type: 'post',
                    url: ctx + '/BaneHospital/DelHospitalById/' + row[0].hos_id,
                    dataType: 'json',
                    async: false,
                    success: function (data) {
                        layer.close(index);
                        if (data.Statu === 0) {
                            layer.msg("删除成功...");
                            $table.bootstrapTable('refresh', options);
                        }
                        else
                            layer.msg(data.Msg, { icon: 5 });
                    }
                });
            }, function () { });
        });
    },
    //编辑、添加
    editForm: function (row) {
        var $title = "添加医院";
        addOrEditUrl = ctx + "/BaneHospital/AddHospital";//新增
        if (row) {
            addOrEditUrl = ctx + "/BaneHospital/EditHospital";//编辑
            $title = "编辑医院";
        }
        //清空表单
        $('#orgFormTable').resetHideValidForm();
        //打开编辑
        layer.open({
            title: [$title, 'font-size:18px;'],
            type: 1,
            content: $('#org_form'),
            scroll: true,//是否显示滚动条、默认不显示
            btn: ['确定', '取消'],
            area: ['600px', '450px'],
            success: function (li, o) {
                if (row) {
                    $('#orgFormTable')[0].reset();//重置表单
                    $('#orgFormTable').LoadForm(row);//表单填充数据
                } 
            },
            yes: function (li, o) {
                var id = $('#user_id').val();
                if (!id) {
                    layer.msg("请选择登录账户！");
                    return false;
                }
                $('#user_name').val($('#user_id').find("option:selected").text());
                if ($('#orgFormTable').valid()) {
                    //验证通过
                    $('#orgFormTable').ajaxSubmit({
                        url: addOrEditUrl,
                        type: "post",
                        dataType: "json",
                        beforeSubmit: function (arr, $form, options) {
                            layer.msg("数据处理中...", { icon: 1, time: 6000 });
                        },
                        success: function (data, status, xhr, $form) {
                            if (data.Statu === 0) {
                                layer.closeAll();
                                $table.bootstrapTable('refresh', options);
                                layer.msg('保存成功', { icon: 1 });
                            }
                            else
                                layer.alert(data.Msg, { icon: 5 });
                        }
                        //,
                        //error: function (xhr, status, error, $form) {
                        //    layer.msg("保存失败~", { icon: 5 });
                        //}
                    });
                }
            },
            cancel: function (li, o) {

            }
        });
    }
}


